<template>
  <div class="final-inspection-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>成品检验</span>
          <div class="header-operations">
            <el-button type="primary" @click="handleCreate">新建检验</el-button>
            <el-button type="success" @click="handleStandard"
              >检验标准</el-button
            >
          </div>
        </div>
      </template>

      <el-form :inline="true" :model="queryForm" class="search-form">
        <el-form-item label="检验单号">
          <el-input
            v-model="queryForm.inspectionNo"
            placeholder="请输入检验单号"
          />
        </el-form-item>
        <el-form-item label="工单号">
          <el-input
            v-model="queryForm.workOrderNo"
            placeholder="请输入工单号"
          />
        </el-form-item>
        <el-form-item label="产品型号">
          <el-input
            v-model="queryForm.productModel"
            placeholder="请输入产品型号"
          />
        </el-form-item>
        <el-form-item label="检验日期">
          <el-date-picker
            v-model="queryForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="检验结果">
          <el-select
            v-model="queryForm.result"
            placeholder="请选择结果"
            clearable
          >
            <el-option label="合格" value="qualified" />
            <el-option label="不合格" value="unqualified" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="inspectionNo" label="检验单号" width="160" />
        <el-table-column prop="workOrderNo" label="工单号" width="160" />
        <el-table-column prop="productModel" label="产品型号" width="120" />
        <el-table-column prop="productName" label="产品名称" min-width="120" />
        <el-table-column prop="batchNo" label="生产批次" width="120" />
        <el-table-column prop="inspector" label="检验人" width="100" />
        <el-table-column prop="inspectionTime" label="检验时间" width="160" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="280" fixed="right">
          <template #default="scope">
            <el-button link type="primary" @click="handleDetail(scope.row)"
              >详情</el-button
            >
            <el-button
              v-if="scope.row.status === 'qualified'"
              link
              type="success"
              @click="handleCertificate(scope.row)"
            >
              合格证
            </el-button>
            <el-button link type="primary" @click="handleReport(scope.row)"
              >检验报告</el-button
            >
            <el-button
              v-if="scope.row.status === 'unqualified'"
              link
              type="warning"
              @click="handleDisposal(scope.row)"
            >
              处理
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 不合格品处理对话框 -->
    <el-dialog
      v-model="disposalDialogVisible"
      title="不合格品处理"
      width="500px"
    >
      <el-form ref="disposalFormRef" :model="disposalForm" label-width="100px">
        <el-form-item label="不合格原因" prop="reason">
          <el-input
            v-model="disposalForm.reason"
            type="textarea"
            rows="3"
            placeholder="请输入不合格原因"
          />
        </el-form-item>
        <el-form-item label="处理方式" prop="method">
          <el-select
            v-model="disposalForm.method"
            placeholder="请选择处理方式"
            style="width: 100%"
          >
            <el-option label="返工" value="rework" />
            <el-option label="报废" value="scrap" />
            <el-option label="让步接收" value="concession" />
          </el-select>
        </el-form-item>
        <el-form-item label="处理意见" prop="suggestion">
          <el-input
            v-model="disposalForm.suggestion"
            type="textarea"
            rows="3"
            placeholder="请输入处理意见"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="disposalDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleDisposalSubmit">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { ElMessage } from "element-plus";

// 查询表单
const queryForm = reactive({
  inspectionNo: "",
  workOrderNo: "",
  productModel: "",
  dateRange: [],
  result: "",
});

// 表格数据
const tableData = ref([
  {
    inspectionNo: "FQC202403150001",
    workOrderNo: "WO202403150001",
    productModel: "XH-2024A",
    productName: "高精度轴承总成",
    batchNo: "BT20240315001",
    inspector: "张三",
    inspectionTime: "2024-03-15 09:30:00",
    status: "qualified",
    remark: "常规检验",
  },
  {
    inspectionNo: "FQC202403150002",
    workOrderNo: "WO202403150002",
    productModel: "XH-2024B",
    productName: "液压控制阀",
    batchNo: "BT20240315002",
    inspector: "李四",
    inspectionTime: "2024-03-15 10:45:00",
    status: "unqualified",
    remark: "外观不合格",
  },
  {
    inspectionNo: "FQC202403150003",
    workOrderNo: "WO202403150003",
    productModel: "XH-2024C",
    productName: "电机控制器",
    batchNo: "BT20240315003",
    inspector: "王五",
    inspectionTime: "2024-03-15 14:20:00",
    status: "processing",
    remark: "检验进行中",
  },
]);

// 分页数据
const total = ref(3);
const currentPage = ref(1);
const pageSize = ref(10);

// 不合格品处理表单
const disposalDialogVisible = ref(false);
const disposalFormRef = ref(null);
const disposalForm = reactive({
  reason: "",
  method: "",
  suggestion: "",
});

// 状态显示
const getStatusType = (status) => {
  const statusMap = {
    processing: "warning",
    qualified: "success",
    unqualified: "danger",
  };
  return statusMap[status];
};

const getStatusText = (status) => {
  const statusMap = {
    processing: "检验中",
    qualified: "合格",
    unqualified: "不合格",
  };
  return statusMap[status];
};

// 处理函数
const handleSearch = () => {
  ElMessage.success("查询成功");
};

const handleReset = () => {
  Object.keys(queryForm).forEach((key) => {
    queryForm[key] = "";
  });
  queryForm.dateRange = [];
};

const handleCreate = () => {
  ElMessage.success("新建检验");
};

const handleStandard = () => {
  ElMessage.success("打开检验标准");
};

const handleDetail = (row) => {
  ElMessage.success(`查看详情：${row.inspectionNo}`);
};

const handleCertificate = (row) => {
  ElMessage.success(`打印合格证：${row.inspectionNo}`);
};

const handleReport = (row) => {
  ElMessage.success(`生成检验报告：${row.inspectionNo}`);
};

const handleDisposal = (row) => {
  disposalDialogVisible.value = true;
  disposalForm.reason = "";
  disposalForm.method = "";
  disposalForm.suggestion = "";
};

const handleDisposalSubmit = () => {
  ElMessage.success("提交成功");
  disposalDialogVisible.value = false;
};

const handleSizeChange = (val) => {
  pageSize.value = val;
};

const handleCurrentChange = (val) => {
  currentPage.value = val;
};
</script>

<style scoped>
.final-inspection-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}

.dialog-footer {
  padding-top: 20px;
  text-align: right;
}
</style>
